<?php

/**
 * Abonnement form base class.
 *
 * @method Abonnement getObject() Returns the current form's model object
 *
 * @package    tfe
 * @subpackage form
 * @author     3emeIG
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseAbonnementForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                   => new sfWidgetFormInputHidden(),
      'libelle'                              => new sfWidgetFormInputText(),
      'saison'                               => new sfWidgetFormChoice(array('choices' => array('2007-2008' => '2007-2008', '2008-2009' => '2008-2009', '2009-2010' => '2009-2010', '2010-2011' => '2010-2011', '2012-2013' => '2012-2013', '2014-2015' => '2014-2015', '2015-2016' => '2015-2016', '2016-2017' => '2016-2017', '2018-2019' => '2018-2019', '2019-2020' => '2019-2020'))),
      'code'                                 => new sfWidgetFormInputText(),
      'nombre_de_spectacles'                 => new sfWidgetFormInputText(),
      'prix_en_prevente'                     => new sfWidgetFormInputText(),
      'prix_en_saison'                       => new sfWidgetFormInputText(),
      'prix_par_semestre'                    => new sfWidgetFormInputText(),
      'prix_pour_membres'                    => new sfWidgetFormInputText(),
      'commentaire'                          => new sfWidgetFormTextarea(),
      'deleted_at'                           => new sfWidgetFormDateTime(),
      'created_at'                           => new sfWidgetFormDateTime(),
      'updated_at'                           => new sfWidgetFormDateTime(),
      'spectacles_list'                      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Spectacle')),
      'courriers_electroniques_envoyes_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CourrierElectronique')),
    ));

    $this->setValidators(array(
      'id'                                   => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'libelle'                              => new sfValidatorString(array('max_length' => 45)),
      'saison'                               => new sfValidatorChoice(array('choices' => array(0 => '2007-2008', 1 => '2008-2009', 2 => '2009-2010', 3 => '2010-2011', 4 => '2012-2013', 5 => '2014-2015', 6 => '2015-2016', 7 => '2016-2017', 8 => '2018-2019', 9 => '2019-2020'), 'required' => false)),
      'code'                                 => new sfValidatorString(array('max_length' => 10, 'required' => false)),
      'nombre_de_spectacles'                 => new sfValidatorInteger(array('required' => false)),
      'prix_en_prevente'                     => new sfValidatorNumber(),
      'prix_en_saison'                       => new sfValidatorNumber(),
      'prix_par_semestre'                    => new sfValidatorNumber(array('required' => false)),
      'prix_pour_membres'                    => new sfValidatorNumber(array('required' => false)),
      'commentaire'                          => new sfValidatorString(array('required' => false)),
      'deleted_at'                           => new sfValidatorDateTime(array('required' => false)),
      'created_at'                           => new sfValidatorDateTime(),
      'updated_at'                           => new sfValidatorDateTime(),
      'spectacles_list'                      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Spectacle', 'required' => false)),
      'courriers_electroniques_envoyes_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'CourrierElectronique', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('abonnement[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Abonnement';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['spectacles_list']))
    {
      $this->setDefault('spectacles_list', $this->object->Spectacles->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['courriers_electroniques_envoyes_list']))
    {
      $this->setDefault('courriers_electroniques_envoyes_list', $this->object->CourriersElectroniquesEnvoyes->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveSpectaclesList($con);
    $this->saveCourriersElectroniquesEnvoyesList($con);

    parent::doSave($con);
  }

  public function saveSpectaclesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['spectacles_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Spectacles->getPrimaryKeys();
    $values = $this->getValue('spectacles_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Spectacles', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Spectacles', array_values($link));
    }
  }

  public function saveCourriersElectroniquesEnvoyesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['courriers_electroniques_envoyes_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CourriersElectroniquesEnvoyes->getPrimaryKeys();
    $values = $this->getValue('courriers_electroniques_envoyes_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CourriersElectroniquesEnvoyes', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CourriersElectroniquesEnvoyes', array_values($link));
    }
  }

}
